package com.itheima.service.impl.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.core.PageInfo;
import com.itheima.mapper.sms.SmsHomeNewProductMapper;
import com.itheima.pojo.pms.PmsBrand;
import com.itheima.pojo.sms.SmsHomeNewProduct;
import com.itheima.service.sms.SmsHomeNewProductService;
import com.itheima.utils.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeNewProductServiceImpl extends ServiceImpl<SmsHomeNewProductMapper, SmsHomeNewProduct> implements SmsHomeNewProductService {

    @Autowired
    private SmsHomeNewProductMapper newProductMapper;

    //批量修改首页新品状态
    @Override
    public void updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        List<SmsHomeNewProduct>list=this.listByIds(ids);
        for (SmsHomeNewProduct smsHomeNewProduct : list) {
            smsHomeNewProduct.setRecommendStatus(recommendStatus);
        }
        this.updateBatchById(list);
    }

    //分页查询
    @Override
    public PageInfo getList(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        Page<SmsHomeNewProduct> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeNewProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(recommendStatus != null, SmsHomeNewProduct::getRecommendStatus, recommendStatus);
        wrapper.like(productName != null, SmsHomeNewProduct::getProductName, productName);
        wrapper.orderByAsc(SmsHomeNewProduct::getSort);

         Page<SmsHomeNewProduct> pmsBrandPage = this.page(page, wrapper);
        int total = (int)pmsBrandPage.getTotal();
        int current = (int) pmsBrandPage.getCurrent();

        //封装分页数据
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(total);
        pageInfo.setTotalPage(current);
        pageInfo.setList(pmsBrandPage.getRecords());

        return pageInfo;

    }

    /**
     * 添加首页新品
     *
     * @param list
     * @return
     */
    @Override
    public void create(List<SmsHomeNewProduct> list) {
        list.stream().forEach(smsHomeNewProduct -> this.save(smsHomeNewProduct));
    }

    /**
     * 批量删除首页新品
     *
     * @param ids
     * @return
     */
    @Override
    public void delete(List<Long> ids) {
        this.removeByIds(ids);
    }

    /**
     * 修改首页新品排序
     *
     * @param sort
     * @param id
     * @return
     */
    @Override
    public void updateSort(Integer sort, Long id) {
        SmsHomeNewProduct newProduct = new SmsHomeNewProduct();
        newProduct.setId(id);
        newProduct.setSort(sort);
        this.updateById(newProduct);
    }
}
